package com.god530.androidutils.service;

import java.io.FileDescriptor;
import java.io.PrintWriter;

import com.god530.androidutils.utils.LogUtils;
import com.god530.androidutils.utils.SystemUtils;
import com.god530.androidutils.utils.TimeTaskUtils;
import com.god530.androidutils.utils.TimeTaskUtils.TimeTaskCallBack;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.IBinder;

public class UtilsService extends Service{

	public static final void startUtilsService(Context ctx) {
		Intent intent = new Intent(ctx, UtilsService.class);
		ctx.startService(intent);
	}
	
	private Thread thread;
	
	private void startThread() {
		stopThread();
		if(thread == null) {
			thread = new Thread(new Runnable(){

				@Override
				public void run() {
					TimeTaskUtils.cyclicity(new TimeTaskCallBack(){

						@Override
						public void callBack() {
							String currentActivityName = SystemUtils.getCurrentActivityName(UtilsService.this);
							LogUtils.d("[UtilsService] currentActivityName = " + currentActivityName);
						}
						
					}, 2*1000);
				}
				
			});
			thread.start();
		}
	}
	
	private void stopThread() {
		if(thread != null) {
			thread.interrupt();
			thread = null;
		}
	}
	
	@Override
	public IBinder onBind(Intent arg0) {
		LogUtils.i("[UtilsService] onBind");
		return null;
	}

	@Override
	protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
		super.dump(fd, writer, args);
		LogUtils.i("[UtilsService] dump");
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		LogUtils.i("UtilsService onConfigurationChanged" );
	}

	@Override
	public void onCreate() {
		super.onCreate();
		LogUtils.i("[UtilsSerivce] onCreate");
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		LogUtils.i("[UtilsService] onDestroy");
		stopThread();
	}

	@Override
	public void onLowMemory() {
		super.onLowMemory();
		LogUtils.i("[UtilsService] onLowMemory");
	}

	@Override
	public void onRebind(Intent intent) {
		super.onRebind(intent);
		LogUtils.i("[UtilsService] onRebind");
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		LogUtils.i("[UtilsService] onStartCommand");
		startThread();
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onTaskRemoved(Intent rootIntent) {
		super.onTaskRemoved(rootIntent);
		LogUtils.i("[UtilsService] onTaskRemoved");
	}

	@Override
	public void onTrimMemory(int level) {
		super.onTrimMemory(level);
		LogUtils.i("[UtilsService] onTrimMemory");
	}

	@Override
	public boolean onUnbind(Intent intent) {
		LogUtils.i("[UtilsService] onUnbind");
		return super.onUnbind(intent);
	}



}
